<?php

// 用户
class yonghuModule extends Model
{
    public function __construct()
    {
        parent::__construct("yonghu");
    }

    public function getdetailByid($id)
    {
        $detail = $this->find("id,mingcheng,touxiang,wx_openid,shoujihao")->findOne($id);
        if ($detail) {
            return $detail;
        } else {
            return [];
        }
    }

    public function initYonghu($id)
    {
        $detail = $this->getdetailByid($id);
        //添加默认收藏夹
        $shoucangjiaM = new shoucangjiaModule();
        $shoucangjiaM->adddefultdataByYonghu($id);
        $shezhiM = new shezhiModule();
        $shezhiM->adddefultvalueByYonghu($id);
    }

    /**
     * 通过微信小程序更新用户的数据
     * @date: 2019年2月16日 下午9:48:48
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function insertOrupdateYonghuByWxInfo($yonghuInfo)
    {
        $data['wx_openid'] = $yonghuInfo['openId'];
        $yonghuDetail = $this->where($data)->one();
        if ($yonghuDetail) {
            return $yonghuDetail;
            //用户存在，返回用户信息
        } else {
            //不存在，则插入用户记录
            $data['mingcheng'] = $yonghuInfo['nickName'];
            $data['wx_info'] = json_encode($yonghuInfo);
            $data['touxiang'] = $yonghuInfo['avatarUrl'];
            $data['dtime'] = date("Y-m-d H:i:s");
            $id = $this->insert($data);
            //初始化用户
            $this->initYonghu($id);
            return $this->getdetailByid($id);
        }
    }

    /**
     * 获取收藏列表
     * @date: 2019年2月27日 下午4:03:12
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getshoucanglistByyonghuId($yonghuid, $shoucangjia_id)
    {
        $zhengcekuM = new zhengcekuModule();
        $rizhiM = new rizhiModule();
        $shoucangjiaM = new shoucangjiaModule();
        $list = $rizhiM->find(" *  ")->where("yonghu='{$yonghuid}' and leixing in (1) and shoucangjia='{$shoucangjia_id}' ")->all();
        $re_list = [];
        foreach ($list as $k => $v) {
            //查询收藏次数
            $shoucangList = $rizhiM->find(" *  ")->where("leixing in (1) and laiyuan='{$v['laiyuan']}' ")->all();
            $shoucangCount = count($shoucangList);

            if ($v['laiyuan'] == "sl_zhengceku") {
                $temp_data = $zhengcekuM->getonelistByid($v['laiyuanbianhao']);
                if ($temp_data) {
                    $temp_data["shoucangleixing"] = $v["laiyuan"];
                    $temp_data["shoucangCount"] = $shoucangCount;
                    $re_list[] = $temp_data;
                }
            } else {
                $tempM = new Model($v["laiyuan"]);
                $temp_data = $tempM->findOne($v["laiyuanbianhao"]);
                if ($temp_data) {
                    $temp_data["shoucangleixing"] = $v["laiyuan"];
                    $temp_data["shoucangCount"] = $shoucangCount;
                    $re_list[] = $temp_data;
                }
            }
        }
        return $re_list;
    }

    /**
     * 查询手机号是否激活
     * @date: 2019年6月18日 下午6:35:32
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function isjihuo($shoujihao)
    {
        $data = $this->where("shoujihao='{$shoujihao}' ")->one();
        if (count($data['jihuo']) > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 过去用户订阅政策的sql
     * @date: 2019年8月21日 下午5:19:02
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getdingyuelanmuSqlByYonghuid($yonghuid)
    {
        $guanjiancitongjiM = new guanjiancitongjiModule();
        $parameterM = new parameterModel();
        $whereStr = "";
        $dingyuejiluArray = $guanjiancitongjiM->getalldingyuelanmuList($yonghuid);
        foreach ($dingyuejiluArray as $k1 => $v1) {
            //拼接政策关键词
            if ($v1["value"] == "2") {
                foreach ($v1["lanmulist"] as $k2 => $v2) {
                    if ($v2["isdingyue"] == false) {
                        continue;
                    }
                    if ($whereStr == "") {
                        $whereStr = " biaoqian like '%{$v2["name"]}%' ";
                    } else {
                        $whereStr = $whereStr . " or " . "biaoqian like '%{$v2["name"]}%' ";
                    }
                }
            }
            //拼接政策扶持方向
            if ($v1["value"] == "1") {
                foreach ($v1["lanmulist"] as $k2 => $v2) {
                    if ($v2["isdingyue"] == false) {
                        continue;
                    }
                    $fuchifangxiangdata = $parameterM->where("u1='{$v2['name']}' and classid in (376) ")->one();
                    if (count($fuchifangxiangdata) > 0) {
                        if ($whereStr == "") {
                            $whereStr = " fuchifangxiang='{$fuchifangxiangdata['u2']}' ";
                        } else {
                            $whereStr = $whereStr . " or " . " fuchifangxiang='{$fuchifangxiangdata['u2']}'  ";
                        }
                    }
                }
            }
        }
        return $whereStr;
    }

}